package com.einmeer.hfbank.service.impl;

import com.einmeer.hfbank.entity.Card;
import com.einmeer.hfbank.entity.Deposit;
import com.einmeer.hfbank.hfconst.CardConst;
import com.einmeer.hfbank.hfconst.TransferAccountsConst;
import com.einmeer.hfbank.mapper.CardMapper;
import com.einmeer.hfbank.mapper.DepositMapper;
import com.einmeer.hfbank.mapper.TransferAccountsMapper;
import com.einmeer.hfbank.service.TransferAccountsService;
import com.einmeer.hfbank.vo.ResultVo;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.sql.Timestamp;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author 芊嵛
 * @date 2024/1/13
 */
@Service
@Transactional
public class TransferAccountsServiceImpl implements TransferAccountsService {
    @Resource
    private TransferAccountsMapper transferAccountsMapper;
    @Resource
    private CardMapper cardMapper;
    @Resource
    private DepositMapper depositMapper;

    @Override
    public ResultVo addTransferAccounts(String collectionName, Long collectionNumber, Integer collectionBankId, String collectionAmount, String commission, Long paymentNumber, int cardPassword) {
        // 判断支付账户的密码是否正确
        Long pa = cardMapper.selectTAByNumberAndPassword(paymentNumber,cardPassword);
        if (pa==null){
            return new ResultVo(TransferAccountsConst.TANO,TransferAccountsConst.PASSWORD);
        }
        // 根据姓名、卡号、开户行、卡的类型查询卡是否存在
        Long n1 = cardMapper.selectTA(collectionName, collectionNumber, collectionBankId, 0);
        // 判断是否存在收款人
        if (n1 != null) {
            Timestamp timestamp = new Timestamp(System.currentTimeMillis());
            // 根据卡号查询金额是否能转账
            Card card = cardMapper.selectCard(paymentNumber);
            BigDecimal a = new BigDecimal(card.getCardCurrent());
            BigDecimal fee = new BigDecimal(commission);
            BigDecimal pay = new BigDecimal(collectionAmount);
            // 复制一份插入到存款表中,为的是实现先后功能,先去款后得到
            BigDecimal pay1 = pay;
            // 支付金额算上手续费
            pay = pay.add(fee);
            System.out.println("a"+a);
            System.out.println("pay"+pay);
            if (a.compareTo(pay) == -1) {
                return new ResultVo(TransferAccountsConst.TANO, TransferAccountsConst.BALANCENO);
            }
            // 排序存款列表
            List<Deposit> depositList = depositMapper.selectAllByCardNumber(paymentNumber, 0);
            // 根据存款id倒序
            depositList = depositList.stream().sorted(Comparator.comparing(Deposit::getDepositId).reversed()).collect(Collectors.toList());
            // 遍历存款id
            for (Deposit temp : depositList) {
                // 取出存款金额
                BigDecimal balance = new BigDecimal(temp.getDepositAmount());
                System.out.println(temp.getDepositId()+"余额"+balance);
                // 如果pay等于0直接退出
                if (pay.compareTo(BigDecimal.valueOf(0)) == 0) {
                    break;
                } else if (balance.compareTo(BigDecimal.valueOf(0)) == 0) {
                    continue;
                }
                // 如果余额大于等于支付
                else if (balance.compareTo(pay) > -1) {
                    balance = balance.subtract(pay);
                    // 更新存款表,次数改为0
                    depositMapper.updateDeposit(temp.getDepositId(), balance.toPlainString(), timestamp, 0);
                    break;
                }
                // 如果余额小于支付
                else if (balance.compareTo(pay) == -1) {
                    // 更新存款表，存款金额改为0，次数改为0
                    depositMapper.updateDeposit(temp.getDepositId(), "0", timestamp, 0);
                    // pay减余额
                    pay = pay.subtract(balance);
                    System.out.println("待支付"+pay);
                }
            }
            depositMapper.insertOne(pay1.toPlainString(),0,timestamp,0,collectionNumber);
            // 判断是否插入成功
            int n2 = transferAccountsMapper.insertTransferAccounts(collectionName, collectionNumber, collectionBankId, collectionAmount, commission, timestamp, paymentNumber);
            if (n2 == 0) {
                return new ResultVo(TransferAccountsConst.TANO, TransferAccountsConst.TAINSERTNO);
            } else {
                return new ResultVo(TransferAccountsConst.TADOK, TransferAccountsConst.TAINSERTOK);
            }
        } else {
            return new ResultVo(TransferAccountsConst.TANO, TransferAccountsConst.TASELECT);
        }

    }
}
